package com.woniuxy.util;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @author HP
 * @version 1.0
 * @project
 * @description
 * @date 2024/4/7 15:31:16
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ResponseData<T> {
    private Integer code;
    private String msg;
    private T data;

    //系统用的常量不要在业务代码中出现 200  ok
    public ResponseData ok(){
        return  new ResponseData(ResponseEnum.SUCCESS.getCode(),
                ResponseEnum.SUCCESS.getMsg(),
                null);
    }

    public ResponseData ok(T data){
        return  new ResponseData(ResponseEnum.SUCCESS.getCode(),
                ResponseEnum.SUCCESS.getMsg(),
                data);
    }


    public ResponseData ok(T data,String msg){
        return  new ResponseData(ResponseEnum.SUCCESS.getCode(),
                msg,
                data);

    }
    //代码出现异常时的方法,代码中的异常情况特别多，比如参数校验异常,账号校验异常,库存不够的异常,
    //
    public ResponseData fail(ResponseEnum responseEnum){
        return  new ResponseData(responseEnum.getCode(),
                responseEnum.getMsg(),
                null);


    }

    public ResponseData fail(T data){
        return  new ResponseData(ResponseEnum.FAIL.getCode(),
                ResponseEnum.FAIL.getMsg(),
                data);
    }
}
